<?php 
/**
 * 路由类
 * @author Administrator
 *
 */
class Route {
	//目录
	public static $work_dir;
	public static $controller;
	public static $module;
	public static $template;
	public function __construct($module,$controller,$work_dir)	{
		self::$controller = $controller;
		self::$work_dir=$work_dir;
		self::$module = $module;

// 		require 'PathConfig.php';

// 		function __autoload($classname) 		{
// 			$pathconfig = new pathconfig();
// 			$path       = $pathconfig->classpath($classname,Route::$work_dir);
// 			require $path;
// 		}
		
		$moduleclasss =ucfirst(self::$module).'Controller';
		if(class_exists($moduleclasss)){
			$run    = new $moduleclasss;
		}else{
			//后台
			if($work_dir==ADMIN_DIR){
				if(empty($_SESSION['admin'])){
					ob_get_clean();
					header("Location:".SITE_URL.$work_dir."/login.html");
				}else{
					header("Location:".SITE_URL.$work_dir."/admin_home.html");
				}
			}else{
				ob_get_clean();
				header('Location:'.SITE_URL);
			}
			//前台
			exit;
		}
		
		$method_exists = false;
		if (method_exists($run,$controller)){
			$this->data = $run->$controller();
			$method_exists = true;
		}
		if (empty(self::$template)){
			$this->template(self::$module,self::$controller);
		}
		if (file_exists(self::$template)){
			$extract_vars = array();
			unset($controller);
			unset($work_dir);
			unset($module);
			unset($moduleclasss);
			unset($run);
			if (isset($this->data)&&is_array($this->data) === TRUE)		{
				extract($this->data);
				$extract_vars = get_defined_vars();
			}
			$template_exists = true;
			
			if (DEV_DEBUG&&!strpos(self::$template,'leftJs')) {
				 echo  "<!-----------------------".self::$template."----------------------------->";
			}		
			require self::$template;
		}
		
		if(!$method_exists&&!$template_exists){//若不存在的方法自动重定向
			ob_get_clean();
			if($work_dir==ADMIN_DIR){
				if (!empty($_COOKIE['5USCHOOL']['admin_id']) && !empty($_COOKIE['5USCHOOL']['admin_pass'])) {
					$admin_id = $_COOKIE['5USCHOOL']['admin_id'];
					$password = $_COOKIE['5USCHOOL']['admin_pass'];
					$row_info = HttpDaoManager::getAdminDao()->getByIdAndPass($admin_id,$password);
					if(!empty($row_info)){
						setcookie($_COOKIE['5USCHOOL']['admin_id'],   '', 1);
						setcookie($_COOKIE['5USCHOOL']['admin_pass'], '', 1);
						header("Location:".SITE_URL.$work_dir."/admin_home.html");
						exit;
					}
				}
				
				if(empty($_SESSION['admin'])){//没登陆、登陆去
					ob_get_clean();
					header("Location:".SITE_URL.$work_dir."/login.html");
				}else{
					header("Location:".SITE_URL.$work_dir."/admin_home.html");
				}
			}else{
				ob_get_clean();				
				header('Location:'.SITE_URL);
			}
			exit;
		}
		
	}
	/**
	 * 模板路径
	 * @param unknown_type $module 模型
	 * @param unknown_type $controller 控制器
	 */
	public function template($module,$controller)	{
		self::$template =WEB_DIR.DIRECTORY_SEPARATOR.'template' .DIRECTORY_SEPARATOR.self::$work_dir. DIRECTORY_SEPARATOR .ClassManager::getToos()->lcfirst($module).DIRECTORY_SEPARATOR. $controller.'.php'; 
	}
	
		
}
?>
